package com.tk.itext7demo.utils.excel;

import com.itextpdf.io.font.otf.GlyphLine;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.Property;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.itextpdf.layout.splitting.DefaultSplitCharacters;

import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: easy
 * @description:
 * @author: tjw
 * @create: 2022-11-10 16:10
 **/
public class Excel2Pdf extends PdfTool{
    protected List<ExcelObject> objects = new ArrayList<>();

    /**
     * <p>Description: 导出单项PDF，不包含目录</p>
     * @param object
     */
    public Excel2Pdf(ExcelObject object, OutputStream out) {
        this.objects.add(object);
        this.out = out;
    }

    /**
     * <p>Description: 导出多项PDF，包含目录</p>
     * @param objects
     */
    public Excel2Pdf(List<ExcelObject> objects , OutputStream out) {
        this.objects = objects;
        this.out = out;
    }

    /**
     * <p>Description: 转换调用</p>
     * @throws MalformedURLException
     * @throws IOException
     */
    public void convert(PageSize pageSize) throws MalformedURLException, IOException {
        Document doc = new Document(getDocument(), pageSize, false);
        getDocument().setDefaultPageSize(pageSize);
        doc.setMargins(20, 20, 20, 20);
        doc.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() {
            @Override
            public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
                return true;//解决word-break: break-all;不兼容的问题，解决纯英文或数字不自动换行的问题
            }
        });
        //Single one
        if(this.objects.size() <= 1){
            Table table = this.toCreatePdfTable(this.objects.get(0), pageSize);
            doc.add(table);
        }

        int numberOfPages = getDocument().getNumberOfPages();
        for (int i = 1; i <= numberOfPages; i++) {
            doc.showTextAligned(new Paragraph(String.format("page %s of %s", i, numberOfPages)),
                    pageSize.getX(), pageSize.getY(), i, TextAlignment.RIGHT, VerticalAlignment.TOP, 0);
        }
        doc.flush();
        //
        getDocument().close();
    }


    private static Rectangle fit(Rectangle img, Rectangle page) {
        float scale = page.getWidth()/img.getWidth();
        return new Rectangle(0,page.getHeight()-img.getHeight()*scale, img.getWidth()*scale, img.getHeight()*scale);
    }

    protected Table toCreatePdfTable(ExcelObject object, PageSize pageSize) throws MalformedURLException, IOException{
        Table table = new PdfTableExcel(object, pageSize).getTable();
        table.setKeepTogether(true);
        return table;
    }

}
